﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using System.Text.RegularExpressions;
namespace ProcessesSMS
{
	/// <summary>
	/// Summary description for SMS_STR.
	/// </summary>
	
	public class SMS_STR
	{								
		public SMS_STR()
		{
			//			
			//
		}				
		public string[] MySplitString(string strContent)
		{
			string strTmp="";
			if (strContent.Length<=160)
			{
				strTmp=strContent;
			}
			else
			{
				if (strContent.Length >160 && strContent.Length<=320)
				{
					strTmp=strContent.Substring(0,160);
					strTmp=strTmp+ "¥" + strContent.Substring(160);									
				}
				if (strContent.Length >320 && strContent.Length<=480)
				{
					strTmp=strContent.Substring(0,160);
					strTmp=strTmp+ "¥" + strContent.Substring(160,160);
					strTmp=strTmp+ "¥" + strContent.Substring(320);
				}			
			}
			string[] strArrayContent=strTmp.Split('¥');			
			return strArrayContent;
		}

		public string ValidMobileNum(string mobilenum)
		{
			mobilenum = mobilenum.Replace(" ","");
			if (mobilenum.Substring(0,1)=="0") mobilenum = "84"+ mobilenum.Substring(1);
			return mobilenum;
			/*if ((mobilenum.Length==10)&(mobilenum.Substring(0,1)=="0")) mobilenum="84" + mobilenum.Substring(1);
			if ((mobilenum.Length==11)&(mobilenum.Substring(0,1)=="0")) mobilenum="84" + mobilenum.Substring(1);
			
			if (mobilenum.Length==11)
			{
				
				if ((mobilenum.Substring(0,4)=="8491")||(mobilenum.Substring(0,4)=="8490")||(mobilenum.Substring(0,4)=="8493")||(mobilenum.Substring(0,4)=="8494")||(mobilenum.Substring(0,4)=="8498")||(mobilenum.Substring(0,4)=="8497")||(mobilenum.Substring(0,5)=="84168"))
				{
					return mobilenum;
				}
				else
				{
					return "";
				}
				
			}
			if (mobilenum.Length==12)
			{
				
				if ((mobilenum.Substring(0,5)=="84168"))
				{
					return mobilenum;
				}
				else
				{
					return "";
				}
				
			}
			else
			{
				return "";
			}*/
		}

		public bool TestMiddle(string str)
		{
			if (str.Length==0)
				return true;
			else
				return false;
		}

		public bool CheckString(string str,string strArray)
		{	bool b;
			string[] strTmpArray=strArray.Split(',');
			b=false;

			for(int i=0;i<strTmpArray.Length;i++){
				if (str.ToUpper()==strTmpArray[i].ToUpper())
					return true;
				else
					b=false;
			}
			return b;
		}
		
		public bool CheckDateTime(string strDateTime)
		{
			try
			{
				if(strDateTime.IndexOf("/")==-1)
					return false;
				string[] arrDateTime = strDateTime.Split('/');
				if(arrDateTime.Length!=3)
					return false;
				int day = int.Parse(arrDateTime[0]);
				int mon = int.Parse(arrDateTime[1]);
				int year = int.Parse(arrDateTime[2]);
				 if (day < 1 || day > 31 || mon > 12 || mon < 1 || year < 1000)
					return false;

				return true;
			}
			catch
			{
				return false;
			}
		}

		public string MyConvertDate(string strTmp,string strFormatDate)
		{
			string strDateTmp="";
			DateTime iaddDate;
			if(strTmp.Length==2) strDateTmp=DateTime.Now.Month + "/" +  strTmp  + "/" + DateTime.Now.Year;
			if(strTmp.Length==4) strDateTmp=strTmp.Substring(2) + "/" + strTmp.Substring(0,2) + "/" + DateTime.Now.Year;
			if(strTmp.Length==8) strDateTmp=strTmp.Substring(2,2) + "/" +strTmp.Substring(0,2)+ "/" +  strTmp.Substring(4);
			if(strTmp.Length==10) strDateTmp=strTmp;
			try
			{
				iaddDate =Convert.ToDateTime(strDateTmp);
			}
			catch{
				throw new Exception("Sai dinh dang ngay thang.");
			}
			return	iaddDate.ToString(strFormatDate);
		}

		public string ReplaceDoubleBlank(string source)
		{
			for (int i = 0; i < source.Length; i++)
			{
				source = source.Replace("  ", " ");
			}
			return source;
		}

		public bool CheckDate(string strTmp)
		{			
			bool b;
			if(strTmp.Length==2||strTmp.Length==4||strTmp.Length==8)
			{
				try
				{					
					b=true;
					if(strTmp.Length==2)
					{
						if (int.Parse(strTmp)>31||int.Parse(strTmp)<1) b=false;
					}
					if(strTmp.Length==4)
					{
						if (int.Parse(strTmp.Substring(2,2))>12||int.Parse(strTmp.Substring(2,2))<1)b=false;
					}
					if(strTmp.Length==8)
					{
						if (int.Parse(strTmp.Substring(4))!=DateTime.Now.Year)b=false;
					}
				}
				catch (Exception)
				{					
					b=false;
				}
				
			}
			else
			{
				b=false;
			}
			return b;
		}
		public bool checkIsNumber(string txNumber)
		{
			bool textIsNumeric = true;
			try
			{
				int.Parse(txNumber.Trim());				
			}
			catch
			{
				textIsNumeric = false;
			}
			return textIsNumeric;
		}
		public bool checkIsDay(string txDate)
		{
			try
			{
				int day = int.Parse(txDate);
				if (day >31) return false;
				return true;
			}
			catch
			{
				return false;
			}
		}
		public bool checkIsDayMonth (string txDate)
		{
			if (txDate.Length !=4) return false;
			int day,month;
			try
			{
				day = int.Parse(txDate.Substring(0,2));
				month = int.Parse(txDate.Substring(2));
			}
			catch
			{
				return false;
			}
			if (month>12) return false;
			if (month ==1 || month ==3 || month == 5 || month ==7 || month ==8 || month == 10 || month ==12)
			{if (day>31) return false;}
			else if (month == 2)
			{
				if (day>29) return false;
			}
			
			else if (day>30) return false;
			return true;
		}

		public bool checkIsFullDate(string txDate)
		{
			int day,month,year;
			if (txDate.Length !=8) return false;
			try
			{
				day = int.Parse(txDate.Substring(0,2));
				month = int.Parse(txDate.Substring(2,2));
				year = int.Parse(txDate.Substring(4));
			}
			catch
			{
				return false;
			}
			if (month>12) return false;
			if (month ==1 || month ==3 || month == 5 || month ==7 || month ==8 || month == 10 || month ==12)
			{if (day>31) return false;}
			else if (month == 2)
			{
				if (year == 0)
				{
					if (day>29) return false;
				}
				else
				{
					if ((year % 400 ==0) || ((year % 100 !=0) & (year % 4 ==0))) //nam nhuan
					{
						if (day>29) return false;
					}
					else
					{
						if (day>28) return false;
					}
				}
			}
			
			else if (day>30) return false;
			return true;
		}
		
		public bool CheckIsEmail(string strEmail)
		{			
			string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
				@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" + 
				@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
			Regex re = new Regex(strRegex);
			if (re.IsMatch(strEmail))
				return (true);
			else
				return (false);

		}
		public int GetZodiac(string txDate)
		{
			int day,month;
			day = int.Parse(txDate.Substring(0,2));
			month = int.Parse(txDate.Substring(2));
			switch (month)
			{
				case (4):
					if (day<20) return 1; 
					else return 2;
				case (5):
					if (day<21) return 2; 
					else return 3;
				case (6):
					if (day<22) return 3; 
					else return 4;
				case (7):
					if (day<23) return 4; 
					else return 5;
				case (8):
					if (day<23) return 5; 
					else return 6;
				case (9):
					if (day<23) return 6; 
					else return 7;
				case (10):
					if (day<24) return 7; 
					else return 8;
				case (11):
					if (day<22) return 8; 
					else return 9;
				case (12):
					if (day<22) return 9; 
					else return 10;
				case (1):
					if (day<20) return 10; 
					else return 11;
				case (2):
					if (day<19) return 11; 
					else return 12;
				case (3):
					if (day<21) return 12; 
					else return 1;
				default:
					return 0;
			}
		}
		public string CoDau2KhongDau(string strIn)
		{
			string tmp = strIn;
			tmp = tmp.Replace('à','a').Replace('ả','a').Replace('ã','a').Replace('á','a').Replace('ạ','a');
			tmp = tmp.Replace('ă','a').Replace('ằ','a').Replace('ẳ','a').Replace('ẵ','a').Replace('ắ','a').Replace('ặ','a');
			tmp = tmp.Replace('â','a').Replace('ầ','a').Replace('ẩ','a').Replace('ẫ','a').Replace('ấ','a').Replace('ậ','a');
			tmp = tmp.Replace('è','e').Replace('ẻ','e').Replace('ẽ','e').Replace('é','e').Replace('ẹ','e');
			tmp = tmp.Replace('ê','e').Replace('ề','e').Replace('ể','e').Replace('ễ','e').Replace('ế','e').Replace('ệ','e');
			tmp = tmp.Replace('ì','i').Replace('ỉ','i').Replace('ĩ','i').Replace('í','i').Replace('ị','i');
			tmp = tmp.Replace('ò','o').Replace('ỏ','o').Replace('õ','o').Replace('ó','o').Replace('ọ','o');
			tmp = tmp.Replace('ô','o').Replace('ồ','o').Replace('ổ','o').Replace('ỗ','o').Replace('ố','o').Replace('ộ','o');
			tmp = tmp.Replace('ơ','o').Replace('ờ','o').Replace('ở','o').Replace('ỡ','o').Replace('ớ','o').Replace('ợ','o');
			tmp = tmp.Replace('ù','u').Replace('ủ','u').Replace('ũ','u').Replace('ú','u').Replace('ụ','u');
			tmp = tmp.Replace('ư','u').Replace('ừ','u').Replace('ử','u').Replace('ữ','u').Replace('ứ','u').Replace('ự','u');
			tmp = tmp.Replace('ỳ','y').Replace('ỷ','y').Replace('ỹ','y').Replace('ý','y').Replace('ỵ','y');
			tmp = tmp.Replace('đ','d');

			tmp = tmp.Replace('À','A').Replace('Ả','A').Replace('Ã','A').Replace('Á','A').Replace('Ạ','A');
			tmp = tmp.Replace('Ă','A').Replace('Ằ','A').Replace('Ẳ','A').Replace('Ẵ','A').Replace('Ắ','A').Replace('Ặ','A');
			tmp = tmp.Replace('Â','A').Replace('Ầ','A').Replace('Ẩ','A').Replace('Ẫ','A').Replace('Ấ','A').Replace('Ậ','A');
			tmp = tmp.Replace('È','E').Replace('Ẻ','E').Replace('Ẽ','E').Replace('É','E').Replace('Ẹ','E');
			tmp = tmp.Replace('Ê','E').Replace('Ề','E').Replace('Ể','E').Replace('Ễ','E').Replace('Ế','E').Replace('Ệ','E');
			tmp = tmp.Replace('Ì','I').Replace('Ỉ','I').Replace('Ĩ','I').Replace('Í','I').Replace('Ị','I');
			tmp = tmp.Replace('Ò','O').Replace('Ỏ','O').Replace('Õ','O').Replace('Ó','O').Replace('Ọ','O');
			tmp = tmp.Replace('Ô','O').Replace('Ồ','O').Replace('Ổ','O').Replace('Ỗ','O').Replace('Ố','O').Replace('Ộ','O');
			tmp = tmp.Replace('Ơ','O').Replace('Ờ','O').Replace('Ở','O').Replace('Ỡ','O').Replace('Ớ','O').Replace('Ợ','O');
			tmp = tmp.Replace('Ù','U').Replace('Ủ','U').Replace('Ũ','U').Replace('Ú','U').Replace('Ụ','U');
			tmp = tmp.Replace('Ư','U').Replace('Ừ','U').Replace('Ử','U').Replace('Ữ','U').Replace('Ứ','U').Replace('Ự','U');
			tmp = tmp.Replace('Ỳ','Y').Replace('Ỷ','Y').Replace('Ỹ','Y').Replace('Ý','Y').Replace('Ỵ','Y');
			tmp = tmp.Replace('Đ','D');
			return tmp;
		}
	}
}
